// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/functions/registerer.h>
#include <pollux/functions/lib/registration_helpers.h>
#include <pollux/functions/prestosql/bitwise.h>

namespace kumo::pollux::functions {
    namespace {
        template<template <class> class T>
        void registerBitwiseBinaryIntegral(const std::vector<std::string> &aliases) {
            register_function<T, int64_t, int8_t, int8_t>(aliases);
            register_function<T, int64_t, int16_t, int16_t>(aliases);
            register_function<T, int64_t, int32_t, int32_t>(aliases);
            register_function<T, int64_t, int64_t, int64_t>(aliases);
        }

        template<template <class> class T>
        void registerBitwiseUnaryIntegral(const std::vector<std::string> &aliases) {
            register_function<T, int64_t, int8_t>(aliases);
            register_function<T, int64_t, int16_t>(aliases);
            register_function<T, int64_t, int32_t>(aliases);
            register_function<T, int64_t, int64_t>(aliases);
        }

        template<template <class> class T>
        void registerShift(const std::vector<std::string> &aliases) {
            register_function<T, int8_t, int8_t, int32_t>(aliases);
            register_function<T, int16_t, int16_t, int32_t>(aliases);
            register_function<T, int32_t, int32_t, int32_t>(aliases);
            register_function<T, int64_t, int64_t, int32_t>(aliases);
        }
    } // namespace

    void registerBitwiseFunctions(const std::string &prefix) {
        registerBitwiseBinaryIntegral<BitwiseAndFunction>({prefix + "bitwise_and"});
        registerBitwiseUnaryIntegral<BitwiseNotFunction>({prefix + "bitwise_not"});
        registerBitwiseBinaryIntegral<BitwiseOrFunction>({prefix + "bitwise_or"});
        registerBitwiseBinaryIntegral<BitwiseXorFunction>({prefix + "bitwise_xor"});
        registerBitwiseBinaryIntegral<BitCountFunction>({prefix + "bit_count"});
        register_function<
            BitwiseArithmeticShiftRightFunction,
            int64_t,
            int64_t,
            int64_t>({prefix + "bitwise_arithmetic_shift_right"});
        registerShift<BitwiseLeftShiftFunction>({prefix + "bitwise_left_shift"});
        registerShift<BitwiseRightShiftFunction>({prefix + "bitwise_right_shift"});
        registerShift<BitwiseRightShiftArithmeticFunction>(
            {prefix + "bitwise_right_shift_arithmetic"});
        register_function<
            BitwiseLogicalShiftRightFunction,
            int64_t,
            int64_t,
            int64_t,
            int64_t>({prefix + "bitwise_logical_shift_right"});
        register_function<
            BitwiseShiftLeftFunction,
            int64_t,
            int64_t,
            int64_t,
            int64_t>({prefix + "bitwise_shift_left"});
    }
} // namespace kumo::pollux::functions
